import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {SideMenuService} from "../../../services/common/side-menu.service";
import {PerformanceProvider} from "../../../providers/perfomance/performance/performance";
import {HttpErrorResponse} from "@angular/common/http";
import {TimeUtil} from "../../../model/util/TimeUtil";
import {PerformanceType} from "../../../model/enum/performance";
import {AchieveDetail} from "../../../model/pages/performance/group-achieve";
import {LoadingService} from "../../../services/common/loading.service";
import {ToggleList} from '../../../model/pages/performance/personal-performance';
import {ToastService} from "../../../services/common/toast.service";
/**
 * Generated class for the PersonalPerformancePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-personal-performance',
    templateUrl: 'personal-performance.html',
    providers: [SideMenuService, PerformanceProvider, LoadingService, ToastService]
})
export class PersonalGroupPerformancePage {
    /**
     * 开始时间
     * @type {string}
     */
    startFilterDate = '';
    /**
     * 结束时间
     * @type {string}
     */
    endFilterDate = '';
    /**
     * id
     * @type {string}
     */
    id = '';
    /**
     * 业绩类型
     * @type {number}
     */
    type: number = PerformanceType.Person;
    /**
     * 业绩详情
     * @type {AchieveDetail}
     */
    personalPerformance: AchieveDetail = null;

    /**
     * 获取数据订阅
     */
    getDataSubscription: any;

    // typeList
    typeList:ToggleList= new ToggleList();

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        private  sideMenuService: SideMenuService,
        private performanceProvider: PerformanceProvider,
        private toastController: ToastController,
        private toastService:ToastService,
        private loadingService: LoadingService) {

    }


    ionViewDidLoad() {
        // console.log('ionViewDidLoad PersonalPerformancePage');
        // this.id = this.navParams.get('id');
        this.type = this.navParams.get('type');
        let month = this.navParams.get("month");

        //初始化时间范围（当月）

        this.startFilterDate = TimeUtil.getLocalTime(Date.now());
        this.endFilterDate = TimeUtil.getLocalTime(Date.now());

        if (month) {
            this.startFilterDate = month;
            this.endFilterDate = month;
        }
        let data = this.navParams.get("data");
        if (data) {
            this.personalPerformance = data;
            if (this.type == PerformanceType.Person) {
                this.id = data.agentCode;
            } else {
                this.id = data.groupId;
            }
        } else {
            /**
             * 开始筛选
             */
            this.filter();
        }
    }

    ionViewWillLeave() {
        // 取消订阅数据
        if (this.getDataSubscription) {
            this.getDataSubscription.unsubscribe();
        }
    }

    /**
     * 返回上一级
     */
    goBack() {
        this.navCtrl.pop();
    }

    openMenu(myEvent) {
        this.sideMenuService.openSideMenu(myEvent)
    }

    /**
     * 开始
     */
    filter() {

        if (this.type == PerformanceType.Person) {
            if (this.endFilterDate != '' && this.startFilterDate != '' && this.endFilterDate < this.startFilterDate) {
                let toast = this.toastController.create({
                    message: '结束时间不能早于开始时间，请重新选择',
                    duration: 2000,
                    position: 'middle'
                });
                toast.present();
                // this.cleanFilterDate();
                return;
            }
        }

        // 显示loading动画
        this.loadingService.showLoading();

        this.getDataSubscription = this.performanceProvider.getAchievement(this.id, this.startFilterDate, this.endFilterDate, this.type).subscribe(data => {

            if (data) {
                if (this.type === PerformanceType.Municipality) {
                    if (data.length > 0) {

                        let array = data.filter(data => {
                            if (data.groupId == this.id) {
                                return true;
                            } else {
                                return false;
                            }
                        });
                        if (array.length > 0) {
                            this.personalPerformance = array[0];
                        }
                    }
                } else {
                    this.personalPerformance = data;
                }
            } else {
                this.personalPerformance = new AchieveDetail();
            }
            this.loadingService.dismissLoading();
        }, (error: HttpErrorResponse) => {
            this.loadingService.dismissLoading();
            this.personalPerformance = new AchieveDetail();
        })

    }

    /**
     * 跳转至成员列表
     * @param {string} id
     * @param {string} month
     */
    goPersonalPerformanceListPage(id: string, month: string) {
        this.navCtrl.push('PersonalPerformanceListPage', {
            id: id,
            month: this.startFilterDate
        })
    }

    toggleType(num){
        this.typeList['type'+num]=!this.typeList['type'+num];
        /*if(this.typeList['type'+num]){
            this.typeList['type'+num]=false
        }else{
            this.typeList['type'+num]=true;
        }*/
    }

}
